package com.zh.common.utils;

import com.zh.common.domain.UserDO;
import com.zh.common.vo.CompanyUserVO;
import com.zh.common.vo.FamilyUserVO;
import com.zh.common.plugins.shiro.utils.JWTUtil;
import com.zh.common.service.UserService;
import com.zh.common.spring.SpringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class ShiroUtils {

    private static UserService userService = (UserService) SpringUtil
            .getBean("userServiceImpl");


    public static Subject getSubjct() {
        return SecurityUtils.getSubject();
    }
    public static UserDO getUser() {
        String token = (String)getSubjct().getPrincipal();
        if(CheckEmptyUtil.isEmpty(token)){
            return null;
        }
        String userName = JWTUtil.getUsername(token);
        int userType = JWTUtil.getUserType(token);
        UserDO vo = userService.getByUserName(userName,userType);
        return vo;
    }
    public static String getUserId() {
        return getUser().getUserId();
    }


    //获取服务商用户
    public static CompanyUserVO getCompanyUser() {
        String token = (String)getSubjct().getPrincipal();
        if(CheckEmptyUtil.isEmpty(token)){
            return null;
        }
        String userName = JWTUtil.getUsername(token);
        int userType = JWTUtil.getUserType(token);
        CompanyUserVO vo = userService.getCompanyUser(userName,userType);
        return vo;
    }

    //获取家庭用户
    public static FamilyUserVO getFamilyUser() {
        String token = (String)getSubjct().getPrincipal();
        if(CheckEmptyUtil.isEmpty(token)){
            return null;
        }
        String userName = JWTUtil.getUsername(token);
        int userType = JWTUtil.getUserType(token);
        FamilyUserVO vo = userService.getFamilyUser(userName,userType);
        return vo;
    }
}
